<?php

use frontend\modules\common\models\BaseParam;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\LinkPager;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\SpecialResourceSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '专题资源';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="hy-special-resource-index">

    <?php Pjax::begin(['id' => 'centerView']); ?>
    <div class="searchBody">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
    </div>
    <div>
        <?= Html::button('批量删除', ['class' => 'btn btn-danger z-remove']) ?>
    </div>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id',
            ],
            'res_id',
            'res_title',
            'type' => [
                'attribute' => 'type',
                'value' => function ($model) {
                    return BaseParam::CodeHelper(BaseParam::$resourceType, $model->type);
                }
            ],
            //'sp_id',
            'created_at',
            //'updated_at',
            //'deleted_at',
        ],
        'pager' => [
            'class' => LinkPager::className(),
            'firstPageLabel' => '首页',
            'prevPageLabel' => '《',
            'nextPageLabel' => '》',
            'lastPageLabel' => '尾页',
            //            'goPageLabel' => true,
            //            'totalPageLable' => '共x页',
            //            'goButtonLable' => 'GO',
            'maxButtonCount' => 5,
            'hideOnSinglePage' => false,
        ]
    ]); ?>
    <?php Pjax::end(); ?>
</div>
<script>
    <?php $this->beginBlock('js_block') ?>
    //批量删除
    $(document).on('click', '.z-remove', function () {

        let id = allChecked();
        if (id) {
            layer.confirm('确认从分馆移除资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-remove')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg(msg['msg'], {icon: 1, time: 1000});
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                        $.pjax.reload({container: "#centerView"});
                    },
                    error: function () {
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg('先选择记录');
        }
    });

    //获取复选框id
    function allChecked() {

        var id = [];
        var dom = $('.grid-view tbody').find('input[type=checkbox]:checked');
        if (dom.length) {
            $.each(dom, function () {
                id.push($(this).val())
            });
        } else {
            return 0;
        }
        return id;
    }
    <?php $this->endBlock() ?>
</script>
<?php $this->registerJs($this->blocks['js_block'], \yii\web\View::POS_END); ?>
